package cn.tedu._02notice.controller;
import cn.tedu._02notice.base.response.jsonresulte;
import cn.tedu._02notice.base.response.statuscode;
import cn.tedu._02notice.mapper.mapper;
import cn.tedu._02notice.pojo.dto.addquery;
import cn.tedu._02notice.pojo.dto.noticeadd;
import cn.tedu._02notice.pojo.dto.update;
import cn.tedu._02notice.pojo.entity.notice;
import cn.tedu._02notice.pojo.vo.noticevo1;
import cn.tedu._02notice.pojo.vo.noticevo2;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Tag(name="资讯管理")
@RestController//@Controller+@ResponseBody
@RequestMapping("/notice")//路径前缀，和类中路径进行拼接
public class noticecontroller
{
    @Autowired
    private mapper mapper;

    /**MYBATIS流程自动装配等待输入
     * 输入数据（来自notice）
     * notice部分数据来自noticeadd
     * noticeadd来自web输入（@PostMapping）
     */
    @Operation(summary = "添加资讯")
    @ApiOperationSupport(order = 10)
    @PostMapping("/api/v1/notice")
//    @Parameters(value = {
//            @Parameter(name = "title", description = "标题", required = true),
//            @Parameter(name = "content", description = "内容", required = true),
//            @Parameter(name = "type", description = "类型", required = true),
//            @Parameter(name = "status", description = "状态", required = true),
//            @Parameter(name = "add",hidden = true)
//    })
//    @ResponseBody
    public jsonresulte getnotice(@RequestBody noticeadd add)
    {
        System.out.println(add);

        notice no=new notice();
        BeanUtils.copyProperties(add,no);
        no.setUserId(1L);
        no.setCreateTime(new Date());
        no.setUpdateTime(new Date());
        mapper.insert(no);
        return new jsonresulte(statuscode.SUCCESS);
    }
    @Operation(summary = "查询资讯")
    @ApiOperationSupport(order = 20)
    @GetMapping("/v2/noticeselect")
//    @ResponseBody
    public jsonresulte noticevo1List(addquery query)
    {
        System.out.println(query);
        List<noticevo1> list=mapper.select(query);
        return new jsonresulte(1010,"成功",list);
    }
@Operation(summary = "删除资讯")
@ApiOperationSupport(order = 30)
    @DeleteMapping("/v2/noticedel")
//    @ResponseBody
@Parameter(name = "id", description = "咨讯编号", required = true)
    public String noticedel(Integer id)
    {
        int bb=mapper.delete(id);
        if(bb>0)
        {
            return "yes";
        }
        return "no";
    }
@Operation(summary = "根据id资讯")
@ApiOperationSupport(order = 40)
    @GetMapping("/v2/noticeselect2")
//    @ResponseBody
@Parameter(name = "id", description = "咨讯编号", required = true)
    public List<noticevo2> noticevo1List(Integer id)
    {
        List<noticevo2> lis=mapper.select2(id);

         return lis;

    }
@Operation(summary = "修改资讯")
@ApiOperationSupport(order = 50)
    @PostMapping("/v2/noticeup")
//    @ResponseBody
    public String noticeupdate(update update)
    {
        notice no=new notice();
        BeanUtils.copyProperties(update,no);
        no.setUserId(1L);
        int lis=mapper.update(no);
        if(lis>0){return "成功";}

        return "不存在";

    }
}
